package com.heima.admin.listener;

import com.heima.admin.service.WemediaNewsAutoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.heima.common.constants.message.NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_TOPIC;

/**
 * 监听自媒体文章审核消息
 */
@Slf4j
@Component
public class WemediaNewsAutoListener {

    @Autowired
    private WemediaNewsAutoService wemediaNewsAutoService;

    /**
     * 监听方法
     */
    @KafkaListener(topics = WM_NEWS_AUTO_SCAN_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            //监听到的消息内容就是自媒体文章id
            Object value = record.value();
            //转为Integer类型的自媒体文章id
            Integer wmNewsId = Integer.parseInt(String.valueOf(value));
            //自动审核自媒体文章
            wemediaNewsAutoService.auditWemediaNews(wmNewsId);
        }

    }

}
